<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="keywords" content="Hexo Theme Keep">
    <meta name="description" content="Hexo Theme Keep">
    <meta name="author" content="Da">
    
    <title>
        
            Java正则表达式学习 |
        
        Da的个人博客
    </title>
    
<link rel="stylesheet" href="/css/style.css">

    <link rel="shortcut icon" href="/images/logo.svg">
    
<link rel="stylesheet" href="/css/font-awesome.min.css">

    <script id="hexo-configurations">
    let KEEP = window.KEEP || {};
    KEEP.hexo_config = {"hostname":"da9527.gitee.io","root":"/","language":"zh-CN","path":"search.json"};
    KEEP.theme_config = {"toc":{"enable":true,"number":false,"expand_all":false,"init_open":false},"style":{"primary_color":"#CC0000","avatar":"/images/avatar.svg","favicon":"/images/logo.svg","article_img_align":"left","left_side_width":"260px","content_max_width":"920px","hover":{"shadow":true,"scale":true},"first_screen":{"enable":true,"background_img":"/images/bg.svg","description":"我不怕千万人阻挡,只怕自己投降"},"scroll":{"progress_bar":{"enable":false},"percent":{"enable":false}}},"local_search":{"enable":true,"preload":false},"code_copy":{"enable":true,"style":"default"},"pjax":{"enable":true},"lazyload":{"enable":true},"version":"3.4.2"};
    KEEP.language_ago = {"second":"%s 秒前","minute":"%s 分钟前","hour":"%s 小时前","day":"%s 天前","week":"%s 周前","month":"%s 月前","year":"%s 年前"};
  </script>
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
<div class="progress-bar-container">
    

    
        <span class="pjax-progress-bar"></span>
        <span class="pjax-progress-icon">
            <i class="fas fa-circle-notch fa-spin"></i>
        </span>
    
</div>


<main class="page-container">

    

    <div class="page-main-content">

        <div class="page-main-content-top">
            <header class="header-wrapper">

    <div class="header-content">
        <div class="left">
            <a class="logo-title" href="/">
                Da的个人博客
            </a>
        </div>

        <div class="right">
            <div class="pc">
                <ul class="menu-list">
                    
                        <li class="menu-item">
                            <a class=""
                               href="/"
                            >
                                首页
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/archives"
                            >
                                归档
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/about"
                            >
                                关于
                            </a>
                        </li>
                    
                    
                        <li class="menu-item search search-popup-trigger">
                            <i class="fas fa-search"></i>
                        </li>
                    
                </ul>
            </div>
            <div class="mobile">
                
                    <div class="icon-item search search-popup-trigger"><i class="fas fa-search"></i></div>
                
                <div class="icon-item menu-bar">
                    <div class="menu-bar-middle"></div>
                </div>
            </div>
        </div>
    </div>

    <div class="header-drawer">
        <ul class="drawer-menu-list">
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/">首页</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/archives">归档</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/about">关于</a>
                </li>
            
        </ul>
    </div>

    <div class="window-mask"></div>

</header>


        </div>

        <div class="page-main-content-middle">

            <div class="main-content">

                
                    <div class="fade-in-down-animation">
    <div class="article-content-container">

        <div class="article-title">
            <span class="title-hover-animation">Java正则表达式学习</span>
        </div>

        
            <div class="article-header">
                <div class="avatar">
                    <img src="/images/avatar.svg">
                </div>
                <div class="info">
                    <div class="author">
                        <span class="name">Da</span>
                        
                            <span class="author-label">Lv4</span>
                        
                    </div>
                    <div class="meta-info">
                        <div class="article-meta-info">
    <span class="article-date article-meta-item">
        <i class="fas fa-edit"></i>&nbsp;2021-06-06 07:57:27
    </span>
    
    

    
    
        <span class="article-wordcount article-meta-item">
            <i class="fas fa-file-word"></i>&nbsp;<span>3.1k 字</span>
        </span>
    
    
        <span class="article-min2read article-meta-item">
            <i class="fas fa-clock"></i>&nbsp;<span>14 分钟</span>
        </span>
    
    
        <span class="article-pv article-meta-item">
            <i class="fas fa-eye"></i>&nbsp;<span id="busuanzi_value_page_pv"></span>
        </span>
    
</div>

                    </div>
                </div>
            </div>
        

        <div class="article-content markdown-body">
            <h1 id="正则表达式"><a href="#正则表达式" class="headerlink" title="正则表达式"></a>正则表达式</h1><p>正则表达式(regular expression)描述了一种字符串匹配的模式（pattern），可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等。</p>
<h2 id="字符匹配符的使用"><a href="#字符匹配符的使用" class="headerlink" title="字符匹配符的使用"></a>字符匹配符的使用</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.regex.Matcher;</span><br><span class="line"><span class="keyword">import</span> java.util.regex.Pattern;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span>: TODO(字符匹配符的使用)</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> da</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2021/6/6 上午 8:03</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RegeXp</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        String str = <span class="string">&quot;aabc123   hdkjha4_54_ADAB.CS(AAS D7$&amp;%@aBc、\n&quot;</span>;</span><br><span class="line"><span class="comment">//        String regStr = &quot;[abcd]&quot;;//匹配 abcd之间的任意一个字符[默认区分大小写]</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;[^abcd]&quot;;//匹配 不是abcd之间的任意一个字符[默认区分大小写]</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;[a-z]&quot;;//匹配a-z之间的任意一个字符</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;[A-Z]&quot;;//匹配A-Z之间的任意一个字符</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;abc&quot;;//匹配 abc[默认区分大小写]</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;(?i)abc&quot;;//匹配 abc[abc不区分大小写]</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;a(?i)bc&quot;;//匹配 abc[bc不区分大小写]</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;a((?i)b)c&quot;;//匹配 abc[b不区分大小写]</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;[^a-z]&quot;;//匹配不在a-z之间的任意一个字符</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;[0-9]&quot;;//匹配 0-9之间的任意一个字符</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;\\d&quot;;//匹配 0-9之间的任意一个字符,相当于[0-9]</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;[^0-9]&quot;;//匹配不是 0-9之间的任意一个字符</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;\\D&quot;;//匹配不是 0-9之间的任意一个字符,相当于[^0-9]</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;[a-zA-Z0-9_]&quot;;//匹配 大小写英文字母 数字 下划线的任意一个字符</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;\\w&quot;;//匹配 大小写英文字母 数字 下划线的任意一个字符,相当于[a-zA-Z0-9_]</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;[^a-zA-Z0-9_]&quot;;//匹配 不是大小写英文字母 数字 下划线的任意一个字符,相当于[^a-zA-Z0-9_]</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;\\W&quot;;//匹配 不是大小写英文字母 数字 下划线的任意一个字符,相当于[^a-zA-Z0-9_]</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;\\s&quot;;//匹配 任何的空白字符(空格,制表符等)</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;\\S&quot;;//匹配 任何的非空白字符(空格,制表符等),效果与\\s相反</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;.&quot;;//匹配 除\n之外的所有字符,如果要匹配.本身则需要转义\\.</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;\\.&quot;;//匹配 .本身</span></span><br><span class="line">        String regStr = <span class="string">&quot;\\(&quot;</span>;<span class="comment">//匹配 ( 需要转义</span></span><br><span class="line">        </span><br><span class="line"><span class="comment">//        Pattern pattern = Pattern.compile(regStr);</span></span><br><span class="line">        Pattern pattern = Pattern.compile(regStr, Pattern.CASE_INSENSITIVE);<span class="comment">//Pattern.CASE_INSENSITIVE,不区分大小写</span></span><br><span class="line"></span><br><span class="line">        Matcher matcher = pattern.matcher(str);</span><br><span class="line">        <span class="keyword">while</span> (matcher.find())</span><br><span class="line">        &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;找到: &quot;</span> + matcher.group(<span class="number">0</span>));</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="选择匹配符和限定符的使用"><a href="#选择匹配符和限定符的使用" class="headerlink" title="选择匹配符和限定符的使用"></a>选择匹配符和限定符的使用</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.regex.Matcher;</span><br><span class="line"><span class="keyword">import</span> java.util.regex.Pattern;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span>: TODO(选择匹配符和限定符的使用)</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> da</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2021/6/6 上午 7:50</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RegeXp</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        String str = <span class="string">&quot;d_a_s@aaaad1111211131111129879!@#$%^&amp;*/&#x27;&#123;. 正则abc$(abc(123(&quot;</span>;</span><br><span class="line"><span class="comment">//        String regStr = &quot;das|\\&#123;|正&quot;;//匹配 das &#123; 正  &quot;|&quot; 选择匹配</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;1&#123;3&#125;&quot;;//匹配 111  相当于 &quot;111&quot;</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;\\d&#123;2&#125;&quot;;//匹配 2位的任意数字</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;\\D&#123;2&#125;&quot;;//匹配 任意非2位数字的2位字串</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;\\w&#123;2&#125;&quot;;//匹配 任意大小写英文字母 数字 下划线组成的2位字串</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;\\W&#123;2&#125;&quot;;//匹配 不是任意大小写英文字母 数字 下划线组成的2位字串</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//      Java匹配默认是贪婪匹配,即尽可能匹配多的</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;a&#123;3,4&#125;&quot;;//匹配 aaa 或者 aaaa</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;\\d&#123;3,4&#125;&quot;;//匹配 任意3个数字或者4个数字的组合</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;\\D&#123;3,4&#125;&quot;;//匹配 任意3个不是数字或者4个不是数字的组合</span></span><br><span class="line"><span class="comment">//        +的使用</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;1+&quot;;//匹配 一个或者多个1(至少出现1次)</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;\\d+&quot;;//匹配 一个或者多个数字(至少出现1次)</span></span><br><span class="line"><span class="comment">//        *的使用</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;1*&quot;;//匹配 0个或者多个1(至少出现0次)</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;\\d*&quot;;//匹配 0个或者多个数字(至少出现0次)</span></span><br><span class="line"><span class="comment">//        ?的使用</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;ab?&quot;;//匹配 a 或者 ab</span></span><br><span class="line">        String regStr = <span class="string">&quot;\\d\\D?&quot;</span>;<span class="comment">//匹配 匹配任意数字 或者 任意数字和字符组成的2位字串</span></span><br><span class="line"></span><br><span class="line">        Pattern pattern = Pattern.compile(regStr);</span><br><span class="line">        Matcher matcher = pattern.matcher(str);</span><br><span class="line"><span class="comment">//        循环取出字段</span></span><br><span class="line">        <span class="keyword">while</span> (matcher.find())</span><br><span class="line">        &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;找到: &quot;</span> + matcher.group(<span class="number">0</span>));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="定位符的使用"><a href="#定位符的使用" class="headerlink" title="定位符的使用"></a>定位符的使用</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.regex.Matcher;</span><br><span class="line"><span class="keyword">import</span> java.util.regex.Pattern;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span>: TODO(定位符的使用)</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> da</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2021/6/6 上午 7:50</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RegeXp</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line"><span class="comment">//        String str = &quot;123abc&quot;;</span></span><br><span class="line">        String str = <span class="string">&quot;hhhhssssshhh hhhhsss hhhss&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//        String regStr = &quot;^[0-9]+[a-z]*&quot;;//匹配 至少以一个数字开头,后接任意个小写字母结尾的字串 123abc</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;^[0-9]+[a-z]+$&quot;;//匹配 至少以一个数字开头,后接任意个小写字母结尾的字串,必须以至少一个小写字母结束</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//        String regStr = &quot;hhh\\b&quot;;//匹配 边界[字符串的最后或者空格的字符串的后面]的hhh</span></span><br><span class="line">        String regStr = <span class="string">&quot;hhh\\B&quot;</span>;<span class="comment">//匹配 边界[字符串的最前面或者空格的字符串的前面]的hhh 与 hhh\\b 相反</span></span><br><span class="line"></span><br><span class="line">        Pattern pattern = Pattern.compile(regStr);</span><br><span class="line">        Matcher matcher = pattern.matcher(str);</span><br><span class="line"><span class="comment">//        循环取出字段</span></span><br><span class="line">        <span class="keyword">while</span> (matcher.find())</span><br><span class="line">        &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;找到: &quot;</span> + matcher.group(<span class="number">0</span>));</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="捕获分组"><a href="#捕获分组" class="headerlink" title="捕获分组"></a>捕获分组</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.regex.Matcher;</span><br><span class="line"><span class="keyword">import</span> java.util.regex.Pattern;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span>: TODO(捕获分组)</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> da</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2021/6/6 上午 7:50</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RegeXp</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        String str = <span class="string">&quot;hhhhdjfasldk sd23423 sdfjk543534&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//        非命名分组</span></span><br><span class="line">        <span class="comment">/**</span></span><br><span class="line"><span class="comment">         *  matcher.group(0) 得到匹配的字符串</span></span><br><span class="line"><span class="comment">         *  matcher.group(1) 得到匹配的字符串的第1个分组的内容</span></span><br><span class="line"><span class="comment">         *  matcher.group(2) 得到匹配的字符串的第2个分组的内容</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;(\\d\\d)(\\d\\d)&quot;;//匹配4个数组的字符串,用()分组</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//        命名分组,为分组命名</span></span><br><span class="line"><span class="comment">//        matcher.group(组名)//通过组名获取分组的内容</span></span><br><span class="line">        String regStr = <span class="string">&quot;(?&lt;g1&gt;\\d\\d)(?&lt;g2&gt;\\d\\d)&quot;</span>;<span class="comment">//匹配4个数组的字符串</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        Pattern pattern = Pattern.compile(regStr);</span><br><span class="line">        Matcher matcher = pattern.matcher(str);</span><br><span class="line"><span class="comment">//        循环取出字段</span></span><br><span class="line">        <span class="keyword">while</span> (matcher.find())</span><br><span class="line">        &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;找到: &quot;</span> + matcher.group(<span class="number">0</span>));</span><br><span class="line">            System.out.println(<span class="string">&quot;第1个分组的内容: &quot;</span> + matcher.group(<span class="number">1</span>));</span><br><span class="line">            System.out.println(<span class="string">&quot;第1个分组的内容[通过组名]: &quot;</span> + matcher.group(<span class="string">&quot;g1&quot;</span>));</span><br><span class="line">            System.out.println(<span class="string">&quot;第2个分组的内容: &quot;</span> + matcher.group(<span class="number">2</span>));</span><br><span class="line">            System.out.println(<span class="string">&quot;第2个分组的内容[通过组名]: &quot;</span> + matcher.group(<span class="string">&quot;g2&quot;</span>));</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="非捕获分组"><a href="#非捕获分组" class="headerlink" title="非捕获分组"></a>非捕获分组</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.regex.Matcher;</span><br><span class="line"><span class="keyword">import</span> java.util.regex.Pattern;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span>: TODO(非捕获分组)</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> da</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2021/6/6 上午 9:46</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RegeXp</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        String str = <span class="string">&quot;张三老师ok 张三家长dsa 张三学生jjgg&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//        String regStr = &quot;张三老师|张三家长|张三学生&quot;;//匹配张三老师或者张三家长或者张三学生</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//        上面的写法可以等价于非捕获分组</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;张三(?:老师|家长|学生)&quot;;//匹配张三老师或者张三家长或者张三学生</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//        String regStr = &quot;张三(?=老师|家长)&quot;;//匹配张三老师或者张三家长里面的张三</span></span><br><span class="line"></span><br><span class="line">        String regStr = <span class="string">&quot;张三(?!老师|家长)&quot;</span>;<span class="comment">//匹配不是张三老师或者张三家长里面的张三(这里是张三同学里的张三)</span></span><br><span class="line"></span><br><span class="line">        Pattern pattern = Pattern.compile(regStr);</span><br><span class="line">        Matcher matcher = pattern.matcher(str);</span><br><span class="line">        <span class="keyword">while</span> (matcher.find())</span><br><span class="line">        &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;找到: &quot;</span> + matcher.group(<span class="number">0</span>));</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="非贪婪匹配"><a href="#非贪婪匹配" class="headerlink" title="非贪婪匹配"></a>非贪婪匹配</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.regex.Matcher;</span><br><span class="line"><span class="keyword">import</span> java.util.regex.Pattern;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span>: TODO(非贪婪匹配)</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> da</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2021/6/6 上午 9:46</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RegeXp</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        String str = <span class="string">&quot;asdasd11111dasd11das3423a1111&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//        String regStr = &quot;\\d+&quot;;//匹配 1个或者多个数字组成的字串,默认是贪婪匹配</span></span><br><span class="line">        String regStr = <span class="string">&quot;\\d+?&quot;</span>;<span class="comment">//非贪婪匹配 这里就相当于匹配单个数字字串</span></span><br><span class="line">        Pattern pattern = Pattern.compile(regStr);</span><br><span class="line">        Matcher matcher = pattern.matcher(str);</span><br><span class="line">        <span class="keyword">while</span> (matcher.find())</span><br><span class="line">        &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;找到: &quot;</span> + matcher.group(<span class="number">0</span>));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p><strong>案例</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.regex.Matcher;</span><br><span class="line"><span class="keyword">import</span> java.util.regex.Pattern;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span>: TODO(正则应用实例)</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> da</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2021/6/6 上午 10:07</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Test</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line"><span class="comment">//        验证字符串是不是纯汉字</span></span><br><span class="line"><span class="comment">//        String str = &quot;玉树临风美少年&quot;;</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;^[\u0391-\uffe5]+$&quot;;// u0391-uffe5代表汉字的范围</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//        验证邮政编码    要求:1-9开头的6位数</span></span><br><span class="line"><span class="comment">//        String str = &quot;123426&quot;;</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;^[1-9]&#123;6&#125;$&quot;;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//        验证qq号     要求以1-9开头的5-11位数</span></span><br><span class="line"><span class="comment">//        String str = &quot;32051&quot;;</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;^[1-9]\\d&#123;4,10&#125;$&quot;;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//        验证手机号     要求必须以13、14、15、18开头的11位数</span></span><br><span class="line">        String str = <span class="string">&quot;13838384383&quot;</span>;</span><br><span class="line"><span class="comment">//        String regStr = &quot;^1[3|4|5|8]\\d&#123;9&#125;$&quot;;</span></span><br><span class="line">        String regStr = <span class="string">&quot;^1(?:3|4|5|8)\\d&#123;9&#125;$&quot;</span>;</span><br><span class="line">         </span><br><span class="line">        Pattern pattern = Pattern.compile(regStr);</span><br><span class="line">        Matcher matcher = pattern.matcher(str);</span><br><span class="line">        <span class="keyword">if</span> (matcher.find()) System.out.println(<span class="string">&quot;找到: &quot;</span> + matcher.group(<span class="number">0</span>));</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="匹配url"><a href="#匹配url" class="headerlink" title="匹配url"></a>匹配url</h2><p><strong>^((http|https)://)?([\w-]+\.)+[\w-]+(/[\w-?&amp;=/%.#_]*)?$</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.regex.Matcher;</span><br><span class="line"><span class="keyword">import</span> java.util.regex.Pattern;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span>: TODO(匹配url)</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> da</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2021/6/6 上午 10:33</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Test</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        String url = <span class="string">&quot;&quot;</span><span class="string">&quot;</span></span><br><span class="line"><span class="string">                     https://www.bilibili.com/video/BV1w64y127iV</span></span><br><span class="line"><span class="string">                     &quot;</span><span class="string">&quot;&quot;</span>;</span><br><span class="line">        <span class="comment">/**</span></span><br><span class="line"><span class="comment">         *  1、先确定url的开头 ^((http|https)://)? //?可以有也可以没有</span></span><br><span class="line"><span class="comment">         *  2、确定www.xxx.xxx格式 ([\w-]+\.)+[\w-]+</span></span><br><span class="line"><span class="comment">         *  3、匹配链接后面带的参数[\w-]+(/[\w-?&amp;=/%.#_]*)?$</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        String regSrt = <span class="string">&quot;^((http|https)://)?([\\w-]+\\.)+[\\w-]+(/[\\w-?&amp;=/%.#_]*)?$&quot;</span>;<span class="comment">//[.?*]表示就是匹配.?*本身</span></span><br><span class="line"></span><br><span class="line">        Pattern pattern = Pattern.compile(regSrt);</span><br><span class="line">        Matcher matcher = pattern.matcher(url);</span><br><span class="line">        <span class="keyword">if</span> (matcher.find()) System.out.println(matcher.group(<span class="number">0</span>));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="Pattern-matches验证字符串是否满足要求"><a href="#Pattern-matches验证字符串是否满足要求" class="headerlink" title="Pattern.matches验证字符串是否满足要求"></a>Pattern.matches验证字符串是否满足要求</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.regex.Pattern;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span>: TODO(演示matches方法, 用于验证输入的字符串是否满足条件)</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> da</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2021/6/6 上午 11:07</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PatternMethod</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        String str = <span class="string">&quot;玉树临风美少年,揽镜自顾夜不眠&quot;</span>;</span><br><span class="line"><span class="comment">//        String regStr = &quot;美少年&quot;;</span></span><br><span class="line">        String regStr = <span class="string">&quot;.*美少年.*&quot;</span>;</span><br><span class="line"><span class="comment">//        整体匹配,判断字符串是否满足要求</span></span><br><span class="line">        <span class="keyword">boolean</span> matches = Pattern.matches(regStr, str);</span><br><span class="line">        System.out.println(<span class="string">&quot;整体匹配 =&gt; &quot;</span> + matches);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="Matcher类的常用方法"><a href="#Matcher类的常用方法" class="headerlink" title="Matcher类的常用方法"></a>Matcher类的常用方法</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.regex.Matcher;</span><br><span class="line"><span class="keyword">import</span> java.util.regex.Pattern;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span>: TODO(Matcher类的常用方法)</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> da</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2021/6/6 上午 11:16</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MatcherMethod</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        String str = <span class="string">&quot;hello tom hello jack hello petty&quot;</span>;</span><br><span class="line">        String regStr = <span class="string">&quot;hello&quot;</span>;</span><br><span class="line">        Pattern compile = Pattern.compile(regStr);</span><br><span class="line">        Matcher matcher = compile.matcher(str);</span><br><span class="line">        <span class="keyword">while</span> (matcher.find())</span><br><span class="line">        &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;---------------&quot;</span>);</span><br><span class="line">            System.out.println(matcher.start());</span><br><span class="line">            System.out.println(matcher.end());</span><br><span class="line"><span class="comment">//            自己分割字符串</span></span><br><span class="line">            System.out.println(<span class="string">&quot;找到: &quot;</span> + str.substring(matcher.start(), matcher.end()));</span><br><span class="line">        &#125;</span><br><span class="line"><span class="comment">//        整体匹配方法</span></span><br><span class="line">        System.out.println(<span class="string">&quot;整体匹配 =&gt; &quot;</span> + matcher.matches());<span class="comment">//false</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//        替换hello为你好,返回值是替换后的字符串,不会修改原来的字符串</span></span><br><span class="line">        String newStr = matcher.replaceAll(<span class="string">&quot;你好&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;原来的字符串: &quot;</span> + str);</span><br><span class="line">        System.out.println(<span class="string">&quot;新的字符串: &quot;</span> + newStr);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="反向引用"><a href="#反向引用" class="headerlink" title="反向引用"></a>反向引用</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.regex.Matcher;</span><br><span class="line"><span class="keyword">import</span> java.util.regex.Pattern;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span>: TODO(反向引用)</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> da</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2021/6/6 上午 11:33</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RegeXp</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        String str = <span class="string">&quot;11222-333444555 3344 5566 11111 5335 1221 5665&quot;</span>;</span><br><span class="line"><span class="comment">//        \\组名(反向引用) 组名从1开始</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;(\\d)\\1&quot;;//找相同的2个数字比如 11 22 33</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;(\\d)\\1&#123;4&#125;&quot;;//找相同的5个连续相同的数字,比如 11111</span></span><br><span class="line"><span class="comment">//        String regStr = &quot;(\\d)(\\d)\\2\\1&quot;;//找个位与千位,十位与百位相同的数,比如 1221</span></span><br><span class="line">        String regStr = <span class="string">&quot;\\d&#123;5&#125;-(\\d)\\1&#123;2&#125;(\\d)\\2&#123;2&#125;(\\d)\\3&#123;2&#125;&quot;</span>;<span class="comment">//找前面是5位数,然后是-,最后是每3个数相等的9位数,比如 11222-333444555</span></span><br><span class="line"></span><br><span class="line">        Pattern pattern = Pattern.compile(regStr);</span><br><span class="line">        Matcher matcher = pattern.matcher(str);</span><br><span class="line">        <span class="keyword">while</span> (matcher.find())</span><br><span class="line">        &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;找到: &quot;</span> + matcher.group(<span class="number">0</span>));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="结巴去重"><a href="#结巴去重" class="headerlink" title="结巴去重"></a>结巴去重</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.regex.Matcher;</span><br><span class="line"><span class="keyword">import</span> java.util.regex.Pattern;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span>: TODO(结巴去重)</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> da</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2021/6/6 下午 10:13</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RegeXp</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        String content = <span class="string">&quot;我...我要...要要要...要学JAVA&quot;</span>;</span><br><span class="line"><span class="comment">//        1、去掉所有的.</span></span><br><span class="line">        Pattern pattern = Pattern.compile(<span class="string">&quot;\\.&quot;</span>);</span><br><span class="line">        Matcher matcher = pattern.matcher(content);</span><br><span class="line">        content = matcher.replaceAll(<span class="string">&quot;&quot;</span>);</span><br><span class="line">        System.out.println(content);</span><br><span class="line"><span class="comment">//        2、去掉重复的字 使用(.)\\1+匹配重字</span></span><br><span class="line"><span class="comment">//        pattern = Pattern.compile(&quot;(.)\\1+&quot;);</span></span><br><span class="line"><span class="comment">//        matcher = pattern.matcher(content);</span></span><br><span class="line"><span class="comment">//        while (matcher.find())</span></span><br><span class="line"><span class="comment">//        &#123;</span></span><br><span class="line"><span class="comment">//            System.out.println(matcher.group(0));</span></span><br><span class="line"><span class="comment">//        &#125;</span></span><br><span class="line"><span class="comment">//        使用反向引用$1 来替换匹配的内容</span></span><br><span class="line"><span class="comment">//        content = matcher.replaceAll(&quot;$1&quot;);</span></span><br><span class="line"><span class="comment">//        System.out.println(content);</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//        使用一条语句 去掉重复的字</span></span><br><span class="line">        content = Pattern.compile(<span class="string">&quot;(.)\\1+&quot;</span>).matcher(content).replaceAll(<span class="string">&quot;$1&quot;</span>);</span><br><span class="line">        System.out.println(content);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


        </div>

        
            <div class="post-copyright-info">
                <div class="article-copyright-info-container">
    <ul>
        <li>本文标题：Java正则表达式学习</li>
        <li>本文作者：Da</li>
        <li>创建时间：2021-06-06 07:57:27</li>
        <li>
            本文链接：https://da9527.gitee.io/2021/06/06/Java正则表达式学习/
        </li>
        <li>
            版权声明：本博客所有文章除特别声明外，均采用 <a class="license" target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">BY-NC-SA</a> 许可协议。转载请注明出处！
        </li>
    </ul>
</div>

            </div>
        

        
            <div class="article-nav">
                
                    <div class="article-prev">
                        <a class="prev"
                           rel="prev"
                           href="/2021/06/06/String%E7%B1%BB%E4%BD%BF%E7%94%A8%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/"
                        >
                            <span class="left arrow-icon flex-center">
                              <i class="fas fa-chevron-left"></i>
                            </span>
                            <span class="title flex-center">
                                <span class="post-nav-title-item">String类使用正则表达式</span>
                                <span class="post-nav-item">上一篇</span>
                            </span>
                        </a>
                    </div>
                
                
                    <div class="article-next">
                        <a class="next"
                           rel="next"
                           href="/2021/06/05/Java%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A0%E4%B9%8Bwebmagic/"
                        >
                            <span class="title flex-center">
                                <span class="post-nav-title-item">Java爬虫学习之webmagic</span>
                                <span class="post-nav-item">下一篇</span>
                            </span>
                            <span class="right arrow-icon flex-center">
                              <i class="fas fa-chevron-right"></i>
                            </span>
                        </a>
                    </div>
                
            </div>
        

        
    </div>
</div>


                
            </div>

        </div>

        <div class="page-main-content-bottom">
            <footer class="footer">
    <div class="info-container">
        <div class="copyright-info info-item">
            &copy;
            
              <span>2020</span>&nbsp;-&nbsp;
            
            2021&nbsp;<i class="fas fa-heart icon-animate"></i>&nbsp;<a href="/">Da</a>
        </div>
        
            <script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
            <div class="website-count info-item">
                
                    <span id="busuanzi_container_site_uv">
                        访问人数&nbsp;<span id="busuanzi_value_site_uv"></span>&ensp;
                    </span>
                
                
                    <span id="busuanzi_container_site_pv">
                        总访问量&nbsp;<span id="busuanzi_value_site_pv"></span>
                    </span>
                
            </div>
        
        <div class="theme-info info-item">
            由 <a target="_blank" href="https://hexo.io">Hexo</a> 驱动&nbsp;|&nbsp;主题&nbsp;<a class="theme-version" target="_blank" href="https://github.com/XPoet/hexo-theme-keep">Keep v3.4.2</a>
        </div>
        
    </div>
</footer>

        </div>
    </div>

    
        <div class="post-tools">
            <div class="post-tools-container">
    <ul class="tools-list">
        <!-- TOC aside toggle -->
        
            <li class="tools-item page-aside-toggle">
                <i class="fas fa-outdent"></i>
            </li>
        

        <!-- go comment -->
        
    </ul>
</div>

        </div>
    

    <div class="right-bottom-side-tools">
        <div class="side-tools-container">
    <ul class="side-tools-list">
        <li class="tools-item tool-font-adjust-plus flex-center">
            <i class="fas fa-search-plus"></i>
        </li>

        <li class="tools-item tool-font-adjust-minus flex-center">
            <i class="fas fa-search-minus"></i>
        </li>

        <li class="tools-item tool-expand-width flex-center">
            <i class="fas fa-arrows-alt-h"></i>
        </li>

        <li class="tools-item tool-dark-light-toggle flex-center">
            <i class="fas fa-moon"></i>
        </li>

        <!-- rss -->
        

        
            <li class="tools-item tool-scroll-to-top flex-center">
                <i class="fas fa-arrow-up"></i>
            </li>
        

        <li class="tools-item tool-scroll-to-bottom flex-center">
            <i class="fas fa-arrow-down"></i>
        </li>
    </ul>

    <ul class="exposed-tools-list">
        <li class="tools-item tool-toggle-show flex-center">
            <i class="fas fa-cog fa-spin"></i>
        </li>
        
    </ul>
</div>

    </div>

    
        <aside class="page-aside">
            <div class="post-toc-wrap">
    <div class="post-toc">
        <ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="nav-text">正则表达式</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AD%97%E7%AC%A6%E5%8C%B9%E9%85%8D%E7%AC%A6%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="nav-text">字符匹配符的使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%80%89%E6%8B%A9%E5%8C%B9%E9%85%8D%E7%AC%A6%E5%92%8C%E9%99%90%E5%AE%9A%E7%AC%A6%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="nav-text">选择匹配符和限定符的使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%9A%E4%BD%8D%E7%AC%A6%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="nav-text">定位符的使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8D%95%E8%8E%B7%E5%88%86%E7%BB%84"><span class="nav-text">捕获分组</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%9D%9E%E6%8D%95%E8%8E%B7%E5%88%86%E7%BB%84"><span class="nav-text">非捕获分组</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%9D%9E%E8%B4%AA%E5%A9%AA%E5%8C%B9%E9%85%8D"><span class="nav-text">非贪婪匹配</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8C%B9%E9%85%8Durl"><span class="nav-text">匹配url</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Pattern-matches%E9%AA%8C%E8%AF%81%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%98%AF%E5%90%A6%E6%BB%A1%E8%B6%B3%E8%A6%81%E6%B1%82"><span class="nav-text">Pattern.matches验证字符串是否满足要求</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Matcher%E7%B1%BB%E7%9A%84%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95"><span class="nav-text">Matcher类的常用方法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8F%8D%E5%90%91%E5%BC%95%E7%94%A8"><span class="nav-text">反向引用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%93%E5%B7%B4%E5%8E%BB%E9%87%8D"><span class="nav-text">结巴去重</span></a></li></ol></li></ol>
    </div>
</div>
        </aside>
    

    <div class="image-viewer-container">
    <img src="">
</div>


    
        <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
          <span class="search-input-field-pre">
            <i class="fas fa-keyboard"></i>
          </span>
            <div class="search-input-container">
                <input autocomplete="off"
                       autocorrect="off"
                       autocapitalize="off"
                       placeholder="搜索..."
                       spellcheck="false"
                       type="search"
                       class="search-input"
                >
            </div>
            <span class="popup-btn-close">
                <i class="fas fa-times"></i>
            </span>
        </div>
        <div id="search-result">
            <div id="no-result">
                <i class="fas fa-spinner fa-pulse fa-5x fa-fw"></i>
            </div>
        </div>
    </div>
</div>

    

</main>




<script src="/js/utils.js"></script>

<script src="/js/main.js"></script>

<script src="/js/header-shrink.js"></script>

<script src="/js/back2top.js"></script>

<script src="/js/dark-light-toggle.js"></script>



    
<script src="/js/local-search.js"></script>




    
<script src="/js/code-copy.js"></script>




    
<script src="/js/lazyload.js"></script>



<div class="post-scripts pjax">
    
        
<script src="/js/left-side-toggle.js"></script>

<script src="/js/libs/anime.min.js"></script>

<script src="/js/toc.js"></script>

    
</div>


    
<script src="/js/libs/pjax.min.js"></script>

<script>
    window.addEventListener('DOMContentLoaded', () => {
        const pjax = new Pjax({
            selectors: [
                'head title',
                '.page-container',
                '.pjax'
            ],
            history: true,
            debug: false,
            cacheBust: false,
            timeout: 0,
            analytics: false,
            currentUrlFullReload: false,
            scrollRestoration: false,
            // scrollTo: true,
        });

        document.addEventListener('pjax:send', () => {
            KEEP.utils.pjaxProgressBarStart();
        });

        document.addEventListener('pjax:complete', () => {
            KEEP.utils.pjaxProgressBarEnd();
            pjax.executeScripts(document.querySelectorAll('script[data-pjax], .pjax script'));
            KEEP.refresh();
        });
    });
</script>



<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/shizuku.model.json"},"display":{"position":"left","width":120,"height":240},"mobile":{"show":false},"log":false});</script></body>
</html>
